/*
 * 
 * LegendShop 多用户商城系统
 * 
 *  版权所有,并保留所有权利。
 * 
 */
package com.legendshop.oa.util;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.displaytag.tags.TableTagParameters;
import org.displaytag.util.ParamEncoder;

import com.legendshop.oa.constants.Constants;
import com.legendshop.oa.constants.DataSortResult;
import com.legendshop.oa.constants.SysParameterEnum;
import com.legendshop.util.AppUtils;

/**
 * 权限控制工具类
 * for web project使用
 */
public class CommonServiceWebUtil {
	
	public static String RESULT ="result";
	
	public static String SORT_NAME ="sortName";


	/**
	 * true: export, false:normal.
	 * 
	 * @param cq
	 *            the cq
	 * @param request
	 *            the request
	 * @return true, if is data for export
	 */
	public static boolean isDataForExport(HttpServletRequest request) {
		String exportValue = request.getParameter(TableTagParameters.PARAMETER_EXPORTING);
		if (AppUtils.isNotBlank(exportValue)) {// 导出
			return true;
		}
		return false;
	}
	
	/**
	 * 是否外部排序.
	 * 
	 * @param cq
	 *            the cq
	 * @param request
	 *            the request
	 * @return true, if is data sort by external
	 */
	public static DataSortResult isDataSortByExternal(HttpServletRequest request) {
		String sortName = request.getParameter((new ParamEncoder("item").encodeParameterName(TableTagParameters.PARAMETER_SORT)));
		String sortOrder = request.getParameter((new ParamEncoder("item").encodeParameterName(TableTagParameters.PARAMETER_ORDER)));
		if(sortName != null){
			return isDataSortByExternal(sortName, sortOrder);
		}else{
			return null;
		}
		
	}
	
	/**
	 * 是否外部排序.
	 * 
	 * @param cq
	 *            the cq
	 * @param request
	 *            the request
	 * @return true, if is data sort by external
	 */
	public static DataSortResult isDataSortByExternal(HttpServletRequest request, String[] expectedCause) {
		String sortName = request.getParameter((new ParamEncoder("item").encodeParameterName(TableTagParameters.PARAMETER_SORT)));
		String sortOrder = request.getParameter((new ParamEncoder("item").encodeParameterName(TableTagParameters.PARAMETER_ORDER)));
		sortName = parseExpectedCause(sortName,expectedCause);
		if(sortName != null){
			return isDataSortByExternal(sortName, sortOrder);
		}else{
			return new DataSortResult();
		}
	}
	
	private static String parseExpectedCause(String sortName, String[] expectedCause){
		if(AppUtils.isBlank(expectedCause)){
			return null;
		}
		for (String cause : expectedCause) {
			if(cause.equals(sortName)){
				return cause;
			}
		}
		return null;
	}


	/**
	 * 是否外部排序.
	 * 
	 * @param cq
	 *            the cq
	 * @param request
	 *            the request
	 * @param map
	 *            the map
	 * @return true, if is data sort by external
	 */
	public static boolean isDataSortByExternal(HttpServletRequest request, Map<String, Object> map) {
		String sortName = request.getParameter((new ParamEncoder("item").encodeParameterName(TableTagParameters.PARAMETER_SORT)));
		if (AppUtils.isNotBlank(sortName)) {
			String sortOrder = request.getParameter((new ParamEncoder("item").encodeParameterName(TableTagParameters.PARAMETER_ORDER)));
			if ("1".equals(sortOrder)) {
				map.put(Constants.ORDER_INDICATOR, "order by " + sortName + " desc");
			} else {
				map.put(Constants.ORDER_INDICATOR, "order by " + sortName + " asc");
			}
			return true;
		}
		return false;
	}
	/**
	 * 
	 * @param request
	 * @param map
	 * @param expectedCause 期望的条件
	 * @return
	 */
	public static boolean isDataSortByExternal(HttpServletRequest request, Map<String, Object> map, String[] expectedCause) {
		if(AppUtils.isBlank(expectedCause)){
			return false;
		}
		
		String sortName = request.getParameter((new ParamEncoder("item").encodeParameterName(TableTagParameters.PARAMETER_SORT)));
		if (AppUtils.isNotBlank(sortName)) {
			String sortOrder = request.getParameter((new ParamEncoder("item").encodeParameterName(TableTagParameters.PARAMETER_ORDER)));
			if ("1".equals(sortOrder)) {
				map.put(Constants.ORDER_INDICATOR, "order by " + sortName + " desc");
			} else {
				map.put(Constants.ORDER_INDICATOR, "order by " + sortName + " asc");
			}
			return true;
		}
		return false;
	}


	// 是否需要验证码
	public static boolean needToValidation(HttpSession session) {
		if (session == null) {
			return false;
		}
		Integer count = (Integer) session.getAttribute(Constants.TRY_LOGIN_COUNT);

		Boolean needToValidation = true;
		if (count == null || count < 3) {
			needToValidation = false;
		}
		Boolean result = PropertiesUtil.getBooleanObject(SysParameterEnum.VALIDATION_IMAGE.name());
		return (result != null && result) && needToValidation;
	}
	
	/**
	 * 排序
	 * @param sortName
	 * @param sortOrder
	 * @return
	 */
	private static DataSortResult isDataSortByExternal(String sortName, String sortOrder) {
		DataSortResult result = new DataSortResult();
		if (AppUtils.isNotBlank(sortName)) {
			if ("1".equals(sortOrder)) {
				result.setOrderIndicator("desc");
			} else {
				result.setOrderIndicator("asc");
			}
			result.setSortName(sortName);
			result.setSortExternal(true);
		}else{
			result.setSortExternal(false);
		}
		return result;
	}
	
	
}
